<template>
  <div class="slider" ref="slider">
    <div class="slider-group" ref="sliderGroup">
      <slot>
      </slot>
    </div>
    <div class="dots">
      <span v-for="(item, index) in dots" :key="index" class="dot" :class="{active: currentPageIndex === index}"></span>
    </div>
  </div>
</template>
<script>
import { addClass } from 'common/js/dom'
import BScroll from 'better-scroll'
export default {
  data() {
    return {
      dots: [],
      // 定义一个变量存储现在的页码
      currentPageIndex: 0
    }
  },
  name: 'slider',
  props: {
    // 由外界来传值是否可以循环,自动播放及循环时间
    loop: {
      type: Boolean,
      default: true
    },
    autoPlay: {
      type: Boolean,
      default: true
    },
    interval: {
      type: Number,
      default: 2000
    }
  },
  mounted() {
    this._setSliderWidth(false)
    setTimeout(() => {
      this._initDots()
      this._initSlider()
      if (this.autoPlay === true) {
        this._autoPlay()
      }
      // 当窗口改变时候要重新计算宽度，初始化better-scroll
      window.addEventListener('resize', () => {
        this._setSliderWidth(true)
        this.slider.refresh()
      })
    }, 20)
  },
  methods: {
    _setSliderWidth(isResize) {
      // 获得有几个div，即几张图片
      this.children = this.$refs.sliderGroup.children
      // 定义总宽度
      let width = 0
      // 获得每张轮播图的宽度
      let sliderWidth = this.$refs.slider.clientWidth
      for (let i = 0; i < this.children.length; i++) {
        let child = this.children[i]
        addClass(child, 'slider-item')
        // 为每张图片所在的div设置宽度
        child.style.width = sliderWidth + 'px'
        // 算出总宽度
        width += sliderWidth
      }
      if (this.loop === true && !isResize) {
        width += 2 * sliderWidth
      }
      this.$refs.sliderGroup.style.width = width + 'px'
    },
    _initSlider() {
      this.slider = new BScroll(this.$refs.slider, {
        scrollX: true,
        scrollY: false,
        momentum: false,
        // 跟源码不一样
        // snap 支持slider组件，自动克隆节点
        snap: {
          // 加这个是为了可以无缝循环
          loop: true,
          // threshold 表示可滚动到下一个的阈值
          threshold: 0.3
        }
      })
      // 给轮播图监听方法，当滑动结束的时候改变currentPageIndex
      this.slider.on('scrollEnd', () => {
        let pageIndex = this.slider.getCurrentPage().pageX
        // pageIndex [0,1,2,3,4]
        // 如果无缝循环播放的时候，源码要减1
        // if(this.loop === true) {
        //   pageIndex -= 1
        // }
        this.currentPageIndex = pageIndex
        if(this.autoPlay === true) {
          this._autoPlay()
        }
      })
      this.slider.on('beforeScrollStart', () => {
        if (this.autoPlay) {
          clearTimeout(this.slider.timer)
        }
      })
    },
    _initDots() {
      this.dots = new Array(this.children.length)
    },
    _autoPlay() {
      this.slider.timer = setTimeout(() => {
        let pageIndex = this.currentPageIndex
        if(pageIndex === 4) {
          this.slider.next(400)
          return
        }
        pageIndex += 1
        this.slider.goToPage(pageIndex, 0, 400)
      }, this.interval)
    }
  }
}
</script>
<style lang="stylus" scoped>
@import '~common/stylus/variable';

.slider {
  position: relative;
  min-height: 1px;
  overflow: hidden;

  .slider-group {
    overflow: hidden;

    .slider-item {
      float: left;
      box-sizing: border-box;
      text-align: center;
      overflow: hidden;

      a {
        display: block;
        width: 100%;
        overflow: hidden;
        text-decoration: none;
      }

      img {
        display: block;
        width: 100%;
      }
    }
  }

  .dots {
    position: absolute;
    bottom: 12px;
    text-align: center;
    width: 100%;

    .dot {
      display: inline-block;
      width: 8px;
      height: 8px;
      margin: 0 4px;
      border-radius: 50%;
      background: $color-text-l;

      &.active {
        width: 20px;
        border-radius: 5px;
        background: $color-text-ll;
      }
    }
  }
}
</style>
